package org.redr.canvas;

import java.io.File;

import com.trolltech.qt.QSignalEmitter;
import com.trolltech.qt.core.Qt;
import com.trolltech.qt.gui.QAction;
import com.trolltech.qt.gui.QApplication;
import com.trolltech.qt.gui.QFileDialog;
import com.trolltech.qt.gui.QIcon;
import com.trolltech.qt.gui.QKeySequence;
import com.trolltech.qt.gui.QLabel;
import com.trolltech.qt.gui.QMenu;
import com.trolltech.qt.gui.QMenuBar;
import com.trolltech.qt.gui.QPalette;
import com.trolltech.qt.gui.QSizePolicy;
import com.trolltech.qt.gui.QToolBar;
import com.trolltech.qt.gui.QWidget;

public class RedRToolbarAndMenu {
	
	RedRCanvas canvas;
	QToolBar toolbar;
	int menuSaveSettingsID = -1;
	int menuSaveSettings = 1;
	QPalette originalPalette = QApplication.palette();
	SearchBox searchBox2 = new SearchBox(null);
	SearchBox searchBox3 = new SearchBox(null);
	QMenu menuRecent;
	
	
	public RedRToolbarAndMenu(RedRCanvas c, QToolBar t){
		canvas = c;
		toolbar = t;
		
		this.initMenu();
		this.initToolbar();
		
	}
	
	public void initToolbar(){
		toolbar.setObjectName("canvasToolbar");
		toolbar.setOrientation(Qt.Orientation.Horizontal);
		// TODO check settings to see if we show the toolbar
		System.out.println("Open png should be here: " + OSPath.join(RedRApplication.parentDir, "coreIcons", "open.png").toString());
		QAction openSchema = toolbar.addAction(new QIcon(OSPath.join(RedRApplication.parentDir, "coreIcons", "open.png").toString()), RedRApplication._("Open Schema"), this, "menuItemOpen()");
		QAction toolSave = toolbar.addAction(new QIcon(OSPath.join(RedRApplication.parentDir, "coreIcons", "save.png").toString()), RedRApplication._("Save Schema"), this, "menuItemSave()");
		
		toolbar.addSeparator();
		QAction showAllWidgets = toolbar.addAction(new QIcon(OSPath.join(RedRApplication.parentDir, "coreIcons", "showAll.png").toString()), RedRApplication._("Show All Widget Windows"), this, "showAllWidgets()");
		QAction closeAllWidgets = toolbar.addAction(new QIcon(OSPath.join(RedRApplication.parentDir, "coreIcons", "closeAll.png").toString()), RedRApplication._("Close All Widget Windows"), this, "closeAllWidgets()");
		
		toolbar.addSeparator();
		QAction printReport = toolbar.addAction(new QIcon(OSPath.join(RedRApplication.parentDir, "coreIcons", "print.png").toString()), RedRApplication._("Generate Report"), this, "menuItemReport()");
	
		toolbar.addSeparator();
		QAction updateAction = toolbar.addAction(new QIcon(OSPath.join(RedRApplication.parentDir, "coreIcons", "update.png").toString()), RedRApplication._("Check For Updates"), this, "menuCheckForUpdates()");
		
		QAction packageAction = toolbar.addAction(new QIcon(OSPath.join(RedRApplication.parentDir, "coreIcons", "packagemanager.png").toString()),  RedRApplication._("Package Manager"), this, "menuOpenPackageManager()");
        
        toolbar.addSeparator();
        QAction toolReloadWidgets = toolbar.addAction(new QIcon(OSPath.join(RedRApplication.parentDir, "coreIcons", "reload.png").toString()), 
        		RedRApplication._("Reload Widgets"), this, "reloadWidgets()");
        
        toolbar.addSeparator();
        QAction toolOptions = toolbar.addAction(new QIcon(OSPath.join(RedRApplication.parentDir, "coreIcons", "options.png").toString()), 
        		RedRApplication._("Red-R Options"), this, "menuItemCanvasOptions()");

        QWidget space = new QWidget();
        space.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding);
        toolbar.addWidget(space);
        
        toolbar.addWidget(new QLabel(RedRApplication._("Search")));
        toolbar.addWidget(searchBox2);
        
        
        readShortcuts();
        readRecentFiles();
	
	
	}
	
	public void initMenu(){
		menuRecent = new QMenu(RedRApplication._("Recent Pipelines"), canvas);
		QMenu menuFile = new QMenu(RedRApplication._("&File"), canvas);
		menuFile.addAction(RedRApplication._("New Pipeline"), this, "menuItemNewSchema()", new QKeySequence(QKeySequence.StandardKey.New));
		menuFile.addAction(new QIcon(OSPath.join(RedRApplication.parentDir, "coreIcons", "open.png").toString()), RedRApplication._("&Open..."), this, "menuItemOpen()", new QKeySequence(QKeySequence.StandardKey.Open));
		menuFile.addAction(RedRApplication._("Import Pipeline"), this, "importSchema()");
		menuFile.addAction(RedRApplication._("Search"), this.searchBox2, "setFocus()", new QKeySequence(Qt.Key.Key_Control.value() + Qt.Key.Key_F.value()));
		menuFile.addSeparator();
		menuFile.addAction(new QIcon(OSPath.join(RedRApplication.parentDir, "coreIcons", "save.png").toString()), RedRApplication._("&Save"), this, "menuItemSave()", new QKeySequence(QKeySequence.StandardKey.Save));
		menuFile.addAction(RedRApplication._("Save &As..."), this, "menuItemSaveAs()", new QKeySequence(QKeySequence.StandardKey.SaveAs));
		menuFile.addAction(RedRApplication._("Save As Template"), this, "menuItemSaveAsTemplate()");
		menuFile.addSeparator();
		
		menuFile.addAction(RedRApplication._("E&xit"), this.canvas, "close()");
		
		QMenu menuOptions = new QMenu(RedRApplication._("&Options"), canvas);
		
		menuOptions.addSeparator();
		
		/*
		 * TODO add section for properties
		 */
		
		QMenu menuTabs = new QMenu(RedRApplication._("&Tabs"), canvas);
		menuTabs.addAction(RedRApplication._("Add New Tab"), canvas, "addTab()");
		menuTabs.addAction(RedRApplication._("Remove Current Tab"), canvas, "removeCurrentTab()");
		
		QMenu packageMenu = new QMenu(RedRApplication._("&Packages"), canvas);
		packageMenu.addAction(RedRApplication._("Package Manager"), this, "menuOpenPackageManager()");
		packageMenu.addAction(RedRApplication._("&Install Package"), this, "menuItemInstallPackage()");
		
		
		QMenu menuHelp = new QMenu(RedRApplication._("&Help"), canvas);
		menuHelp.addAction(RedRApplication._("Red-R Online Help"), this, "menuOpenOnlineHelp()");
		menuHelp.addAction(RedRApplication._("About Red-R"), this, "menuItemAboutRedR()");
		
		QMenuBar menuBar = new QMenuBar(canvas);
		menuBar.addMenu(menuFile);
		menuBar.addMenu(menuOptions);
		menuBar.addMenu(menuTabs);
		menuBar.addMenu(packageMenu);
		menuBar.addMenu(menuHelp);
		canvas.setMenuBar(menuBar);
		
	}
	
	public void menuItemNewSchema(){
		// TODO
	}
	
	public void menuItemSaveAsTemplate(){
		// TODO
	}
	
	public void menuOpenPackageManager(){
		// TODO
	}
	
	public void menuItemInstallPackage(){
		// TODO
	}
	
	public void menuOpenOnlineHelp(){
		// TODO
	}
	
	public void menuItemAboutRedR(){
		// TODO
	}
	
	public void menuItemReport(){
		// TODO
	}
	
	public void menuCheckForUpdates(){
		// TODO
	}
	
	public void menuItemCanvasOptions(){
		// TODO
	}
	
	public void showAllWidgets(){
		// TODO
	}
	
	public void closeAllWidgets(){
		// TODO
	}
	
	public void importSchema(){
		
		String file = QFileDialog.getOpenFileName(canvas, 
				RedRApplication._("Import File"), 
				RedRApplication.coreSettings().getSetting("workingDir"), 
				new QFileDialog.Filter(RedRApplication._("Red-R Widget Schema (*.rrs *.rrts)"))
				);
		if (file == null) return;
		File f = new File(file);
		RedRApplication.coreSettings().setSetting("workingDir", f.getParent());
		RedRApplication.loadDocument(f);
		addToRecentMenu(f.toString());
		
	}
	
	public void menuItemOpen(){
		String file = QFileDialog.getOpenFileName(canvas,
				RedRApplication._("Open File"),
				RedRApplication.coreSettings().getSetting("workingDir"),
				new QFileDialog.Filter(RedRApplication._("Red-R Widget Schema (*.rrs *.rrts)")));
		if (file == null) return;
		File f = new File(file);
		RedRApplication.coreSettings().setSetting("workingDir", f.getParent());
		
		RedRApplication.clearCanvas();
		RedRApplication.loadDocument(f);
		addToRecentMenu(f.toString());
	}
	
	public void menuItemOpenLastSchema(){
		
	}
	
	public void menuItemSave(){
		RedRApplication.saveDocument();
	}
	
	public void reloadWidgets(){
		// TODO add code to reload the widgets in the registry
	}
	
	public void menuItemSaveAs(){
		RedRApplication.saveDocumentAs();
	}
	
	public void menuItemSaveTemplate(){
		RedRApplication.saveTemplate();
	}
	
	public void readRecentFiles(){
		// TODO ?? replace with functions in coreSettings XML structure?
	}
	
	public void openRecentFile(){
		// TODO logic for loading file
		QAction sender = (QAction) QSignalEmitter.signalSender();
		RedRApplication.loadDocument(new File((String) sender.data()));
	}
	
	public void addToRecentMenu(String s){
		File sfile = new File(s);
		if (sfile.exists()){
			QAction newAction = menuRecent.addAction(sfile.getName(), this, "openRecentFile()");
			newAction.setData(s);
		}
	}
	
	public void updateStyle(){
		
	}
	
	public void readShortcuts(){
		
	}

}
